var sel = null;
var wait = true;
var json_use = {};

jQuery(function($){
	$('#btnSubmit').click(function(event){
		event.stopPropagation();
		var json_arr = travel_ul(null);
		if(is_empty(json_arr))return false;
		var json = $.json.encode(json_arr);
		$('#json').val(json);
		$('#iForm').trigger('submit');
	});
		
	$('#btnChild').click(function(event){
		event.stopPropagation();
		if(wait == true)return false;
		if(sel == null)return false;
		var uls = sel.parents('ul').size();
		if(sel.find('span[class="label"]').text() == '')return false;
		if(uls > 3)return false;
		if(is_empty(json_left))return false;
		wait = true;
		if (sel.children('ul').size() == 0){
			sel.append('<ul></ul>');
		}
		sel.children('ul').append('<li>'+get_li()+'</li>');
	});

	$('#btnDel').click(function(event){
		event.stopPropagation();
		if(wait == true)return false;
		if (sel == null)
			return false;
		if(sel.attr('id') == 'root')return false;
		var select = sel.find('select');
		var ul = sel.parent('ul');
		sel.remove();
		sel = null;
		if (ul.attr('id') != 'member' && ul.children('li').size() == 0){
			ul.remove();
		}
		travel_ul(null);
		return false;
	});
			
	$('#member li').live('click', function(event){
		event.stopPropagation();
		sel = $(this);
		$('#member li p').css('background', '#FFF');
		$(this).children('p:first').css('background', '#0CF');
		return false;
	});
			
	$('#member li span[class="label"]').live('dblclick', function(event){
		event.stopPropagation();
		if(wait == true)return false;
		if(is_empty(json_left))return false;
		wait = true;
		$(this).hide().text('');
		var select = $(this).siblings('select');
		select.show();
		make_select_options(select);
		select.blur(function(){$(this).trigger('change');});	// live 不支持此事件，只好在这里绑定
		return false;
	});
	
	$('#member li select').live('change', function(event){
		event.stopPropagation();
		if($(this).find("option:selected").val() == '')return false;
		wait = false;
		$(this).siblings('span').text($(this).find("option:selected").text()).show();
		$(this).hide();
		travel_ul(null);
		return false;
	}).live('mousedown', function(event){
		event.stopPropagation();
		sel = $(this).parent().parent();
		$(this).parent().css('background', '#0CF');
	});
			
	/*$(document).click(function(){
		$('#member li span[class="label"]:hidden').each(function(){
			if($(this).text() != ''){
				var select = $(this).siblings('select');
				$(this).show().text(select.find("option:selected").text());
				select.hide();
			}
		});
	});*/

	if(tree != -1 && typeof(tree) == 'object'){
		var ul = $('#member');
		var uls = parse_ul(tree, ul);
		ul.empty().html(uls);
		travel_ul(null);
		wait = false;
	}
});

function travel_ul(obj){
	var lis = null, json_arr = {};
	if (obj == null){
		obj = $('#member');
		json_use = {};
		json_left = {};
		for(var xxx in json_managers){
			json_left[xxx] = json_managers[xxx];
		}
	}
	lis = obj.children('li');
	var i = 1;
	lis.each(function(){
				var key = $(this).find('select:first').val();
				var val = $(this).find('select:first').find("option:selected").text();
				json_use[key] = val;
				json_arr[key] = {name:val};
				delete json_left[key];
				if ($(this).children('ul').size() > 0){
					json_arr[key].sub = travel_ul($(this).children('ul'));
				} 
			});
	return json_arr;
}

function parse_ul(obj, ul){
	var a = [];
	for(var xxx in obj){
		a.push('<li><p><span class="label">'+obj[xxx].name+'</span><select style="display:none;">');
		a.push('		<option value="'+xxx+'">'+obj[xxx].name+'</option></select></p>');
		if(typeof(obj[xxx].sub) == 'object'){
			a.push('<ul>');
			a.push(parse_ul(obj[xxx].sub));
			a.push('</ul>');
			
		}
		a.push('</li>');	
	}
	return a.join('\n');
}

function make_select_options(select){
	var option = select.find("option:selected");
	var val = option.val();
	var txt = option.text();
	select.empty().append('<option value="'+val+'">'+txt+'</option>');
	for(var xxx in json_left){
		if(xxx == val){
			continue;
		}
		select.append('<option value="'+xxx+'">'+json_left[xxx]+'</option>');
	}
	
	eval('var obj = {'+val+':"'+txt+'"}');
	$.extend(json_left, obj);
	select.focus();
}

function get_li(e){
	var a = [];
	a.push('<p><span class="label"></span><select class="select">');
	a.push('		<option value=""></option>');
	for(var xxx in json_left){		
		a.push('		<option value="'+xxx+'">'+json_left[xxx]+'</option>');
	}
	a.push('		</select></p>');	
	return a.join('\n');
}

function is_empty(obj){
	for(var xxx in obj){
		if(typeof(xxx) != undefined && xxx != '')return false;
	}
	return true;
}